#coding: utf-8

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect

from models.models import Template

from google.appengine.api import users

def index(request):
    return render_to_response('index.html', {})

#@request_login_gmail
def index_old(request):
    user = users.GetCurrentUser()
    if not user:
        greeting = ("<a href=\"%s\">需要登录Google帐号，点击进行登录</a>." % 
                    users.create_login_url("/"))
                
        return render_to_response('msg.html', {'msg':greeting, 'url':'/'})

    key = request.GET.get('key', 0)

    fck = {'value': r''}

    templateList = []
    template = {}
    template['delete'] = 'none'
    template['wksht_name'] = 'Sheet1'
    template['sender'] = user.email()
    template['reply_to'] = user.email()
    template['glogin_mail'] = user.email()
    
    userTemp = Template.gql("WHERE owner =:owner", owner = user)
    
    for temp in userTemp:
        #if temp.owner != user: continue
        t = {}
        t['id'] = temp.key()
        t['name'] = temp.temp_name
        templateList.append(t)
        if key == str(temp.key()):
            template['macro'] = temp.marco
            template['subject'] = temp.title
            template['reply_to'] = temp.reply_tomail
            template['mail_list'] = temp.mail_list
            template['glogin_mail'] = temp.glogin_mail
            template['gFileName'] = temp.gfile_name
            template['wksht_name'] = temp.gwksht_name
            template['id'] = temp.key()
            template['delete'] = ''
            fck['value'] = temp.mail_context
            if temp.glogin_mail:
                template['usedgdata'] = 'checked="CHECKED"'
    
    return render_to_response('editmail.html', {"nickname":user.nickname(),'templateList': templateList,'fck': fck, "template":template})
#end index()


def logout(request):
    return HttpResponseRedirect(users.create_logout_url('/about/'))
    
def about(request):
    return render_to_response('about.html', {})
    
if __name__ == '__main__':
    pass
